//
//  StatusCellTopView.swift
//  我的微博
//
//  Created by ZhangChao on 15/9/8.
//  Copyright © 2015年 ZhangChao. All rights reserved.
//

import UIKit
import SDWebImage

class StatusCellTopView: UIView {

    
    var statusViewModel : StatusViewModel?{
        didSet{
            // 更新数据
            nameLabel.text = statusViewModel?.status.user?.name
            
            // 用SDWebImage加载头像图片
            iconView.sd_setImageWithURL(statusViewModel?.userIconUrl)
            
            // 认证等级
            vipView.image = statusViewModel?.userVipImage
            
            // member等级
            memberView.image = statusViewModel?.userMemberLevel
            
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    private func setupUI(){
        
        backgroundColor = UIColor.whiteColor()
        // 添加控件
        addSubview(iconView)
        addSubview(nameLabel)
        addSubview(memberView)
        addSubview(timeLabel)
        addSubview(sourceLabel)
        addSubview(vipView)
        
        // 自动布局
        iconView.ff_AlignInner(type: ff_AlignType.TopLeft, referView: self, size: CGSize(width: statusCellWidth, height: statusCellWidth), offset: CGPoint(x: statusCellMargin, y: statusCellMargin))
        
        nameLabel.ff_AlignHorizontal(type: ff_AlignType.TopRight, referView: iconView, size: nil, offset: CGPoint(x: statusCellMargin, y: 0))
        
        memberView.ff_AlignHorizontal(type: ff_AlignType.TopRight, referView: nameLabel, size: nil, offset: CGPoint(x: statusCellMargin, y: 0))
        
        timeLabel.ff_AlignHorizontal(type: ff_AlignType.BottomRight, referView: iconView, size: nil, offset: CGPoint(x: statusCellMargin, y: 0))
        
        sourceLabel.ff_AlignHorizontal(type: ff_AlignType.TopRight, referView: timeLabel, size: nil, offset: CGPoint(x: statusCellMargin, y: 0))
        
        vipView.ff_AlignInner(type: ff_AlignType.BottomRight, referView: iconView, size: nil, offset: CGPoint(x: 8, y: 8))
    }
    
    lazy var iconView : UIImageView = UIImageView(image: UIImage(named: "avatar_default_big"))
    lazy var nameLabel : UILabel = UILabel(title: "姓名", color: UIColor.darkGrayColor(), fontSize: 14)
    lazy var memberView : UIImageView = UIImageView(image: UIImage(named: "common_icon_membership_level1"))
    lazy var timeLabel : UILabel = UILabel(title: "刚刚", color: UIColor.lightGrayColor(), fontSize: 10)
    lazy var sourceLabel : UILabel = UILabel(title: "来自 爆米花iPhone20", color: UIColor.lightGrayColor(), fontSize: 10)
    lazy var vipView : UIImageView = UIImageView(image: UIImage(named: "avatar_grassroot"))
}

